package com.iwinding.ladygg.ui.fragment;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;

import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.ui.EaseContactListFragment;
import com.hyphenate.easeui.utils.EaseCommonUtils;
import com.iwinding.ladygg.R;
import com.iwinding.ladygg.constant.EMConstant;
import com.iwinding.ladygg.net.RequestManager;
import com.iwinding.ladygg.net.callback.ContactsCallback;
import com.iwinding.ladygg.net.response.UserRp;
import com.iwinding.ladygg.ui.act.ChatActivity;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;

/**
 * 联系人模块
 * Created by hezhiyong on 2017/3/13.
 */

public class ContactsFragment extends EaseContactListFragment {


    private static final String TAG = ContactsFragment.class.getSimpleName();
    private View loadingView;

    @SuppressLint("InflateParams")
    @Override
    protected void initView() {
        super.initView();
        //add loading view
        loadingView = LayoutInflater.from(getActivity()).inflate(R.layout.em_layout_loading_data, null);
        contentContainer.addView(loadingView);

        registerForContextMenu(listView);
    }

    public boolean isLoggedIn() {
        return EMClient.getInstance().isLoggedInBefore();
    }

    @Override
    public void refresh() {
        RequestManager.postContacts(getActivity(), new ContactsCallback() {
            @Override
            public void onResult(List<UserRp> userRps) {
                Map<String, EaseUser> users = new Hashtable<String, EaseUser>();
                for (UserRp userRp : userRps) {
                    String username = userRp.userId;
                    String nickName = TextUtils.isEmpty(userRp.nickName) ? (TextUtils.isEmpty(userRp.uname) ? username : userRp.uname) : userRp.nickName;
                    String avatar = userRp.headImg;
                    EaseUser user = new EaseUser(username);
                    user.setNickname(nickName);
                    user.setAvatar(avatar);
                    if (username.equals(EMConstant.NEW_FRIENDS_USERNAME) || username.equals(EMConstant.GROUP_USERNAME)
                            || username.equals(EMConstant.CHAT_ROOM) || username.equals(EMConstant.CHAT_ROBOT)) {
                        user.setInitialLetter("");
                    } else {
                        EaseCommonUtils.setUserInitialLetter(user);
                    }
                    users.put(username, user);
                }
                setContactsMap(users);
                getContactList();
                contactListLayout.refresh();
            }

            @Override
            public void onFail(String errorMsg) {

            }
        });
        super.refresh();
    }


    @SuppressWarnings("unchecked")
    @Override
    protected void setUpView() {
        super.setUpView();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                EaseUser user = (EaseUser) listView.getItemAtPosition(position);
                if (user != null) {
                    String username = user.getUsername();
                    // demo中直接进入聊天页面，实际一般是进入用户详情页
                    startActivity(new Intent(getActivity(), ChatActivity.class).putExtra("userId", username));
                }
            }
        });
        hideTitleBar();

//        getActivity().runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                refresh();
//            }
//        });

        // 进入添加好友页
//        titleBar.getRightLayout().setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                startActivity(new Intent(getActivity(), AddFriendActivity.class));
//            }
//        });
//        refresh();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
